<?php
	
	$logid=GetLoginUserID();
	if(isset($_GET['uid'])&&intval($_GET['uid'])>0)
	{
		$uid=intval($_GET['uid']);
	}
	else
	{
		if($logid!=false)
			$uid=$logid;
		else
			edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));
	}
	
	$userDAO=GetUserDAO();
	$user=$userDAO->GetUserNameByID($uid);
	if(!$user)
		edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));

	//Get collect type
	if(isset($_GET['type']))
		$type=intval($_GET['type']);
	else 
	   	$type='1';	
	   	
	if(edp_is_empty_string($type)||$type<1||$type>3)
		$type='1';
		
	//tag
	if(isset($_GET['tag']))
		$tag=edp_db_input(trim($_GET['tag']));		
	else 
	 	$tag='';

	//cid
	if(isset($_GET['cid']))
		$cid=intval($_GET['cid']);
	else 
	 	$cid='0';
			
	//page_info
	$perpage='20';
	$page='1';
	if(isset($_GET['p'])&& is_numeric($_GET['p']) )
	{
		$page=intval($_GET['p']);
	}
	
		
	$collectionDAO=GetCollectionDAO();
	
	switch($type)
	{
		//Shop collect
		case '1':
		{
			$shopDAO = GetShopDAO();
			//Get all shop tags
			$tagList=$collectionDAO->GetShopTagsByUid($uid);
			if($tagList!=false)
			{
				foreach($tagList as $t)
					$tagCount[$t]=$collectionDAO->GetShopCountByTag($uid, $t);
			}
			
			//Get all shop categories
			$cateList=$collectionDAO->GetShopCateByUid($uid);
			if($cateList!=false)
			{
				foreach($cateList as $cate)
					$cateCount[$cate['id']]=$collectionDAO->GetShopCountByCid($uid, $cate['id']);
			}

			//Get tag parameter from URL
			if(!edp_is_empty_string($tag))
			{
				//Get collecting shop according to tag
				$itemCount=$collectionDAO->GetShopCountByTag($uid, $tag);
				$itemList=$collectionDAO->GetShopByTag($uid, $tag, $page, $perpage);
				break;
			}
			else
			{
				//Get collecting shop according to cid
				$categoryDAO=GetCatgoryDAO();
				$category=$categoryDAO->GetShopCateNameByCid($cid);
				if($category=='')
					$cid='-1';
				$itemCount=$collectionDAO->GetShopCountByCid($uid, $cid);
				$itemList=$collectionDAO->GetShopByCid($uid, $cid, $page, $perpage);
				break;
			}
		}
		//Product collect
		case '2':
		{
			//Get all product tags
			$tagList=$collectionDAO->GetProductTagsByUid($uid);
			if($tagList!=false)
			{
				foreach($tagList as $t)
					$tagCount[$t]=$collectionDAO->GetProductCountByTag($uid, $t);
			}
			
			//Get all product categories
			$cateList=$collectionDAO->GetProductCateByUid($uid);
			if($cateList!=false)
			{
				foreach($cateList as $cate)
					$cateCount[$cate['id']]=$collectionDAO->GetProductCountByCid($uid, $cate['id']);
			}
				
			//Get tag parameter from URL
			if(!edp_is_empty_string($tag))
			{
				//Get collecting shop according to tag
				$itemCount=$collectionDAO->GetProductCountByTag($uid, $tag);
				$itemList=$collectionDAO->GetProductByTag($uid, $tag, $page, $perpage);
				break;
			}
			else
			{
				//Get collecting product according to cid
				$categoryDAO=GetCatgoryDAO();
				$category=$categoryDAO->GetProductCateNameByCid($cid);
				if($category=='')
					$cid=-1;
				$itemCount=$collectionDAO->GetProductCountByCid($uid, $cid);
				$itemList=$collectionDAO->GetProductByCid($uid, $cid, $page, $perpage);
				break;
			}
		}
		//List collect
		case '3':
		{
			//Get all list tags
			$tagList=$collectionDAO->GetListTagsByUid($uid);
			if($tagList!=false)
			{
				foreach($tagList as $t)
					$tagCount[$t]=$collectionDAO->GetListCountByTag($uid, $t);
			}
				
			if(edp_is_empty_string($tag))
				$tag='';//Get ALL 		
			$itemCount=$collectionDAO->GetListCountByTag($uid, $tag);
//			if($page<=0||$page>($itemCount/$perpage+1))
//				$page=1;
			$itemList=$collectionDAO->GetListByTag($uid, $tag, $page, $perpage);
			break;
		}
		default:
			break;
	}
		
	
	$currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
	$currentURL = preg_replace( '/&p=[0-9]+/', "", $currentURL );
		
	/*
	 * Page bar
	 */
	
	require_once ( LIB_DIR.'classes/Pager.php' );
	$pageObj = new Pager($currentURL,$itemCount, $perpage, $page, UP_RESULT_PAGE );
	
	
?>
